#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class ConductInfoProvider : CY.GFive.Core.Providers.DALProvider.IConductInfoProvider
	{
		#region Sql
		private static readonly string SqlSelectConductInfo = "SELECT [Id], [StdCode],[YearNum],[Grade],[Term],[CndtScore],[CndtPeerCode],[CndtPeerName],[CndtWeek],[CndtType] FROM [ConductInfo] WHERE  [Id]=@Id";

		private static readonly string SqlInsertConductInfo = "INSERT INTO [ConductInfo]([StdCode],[YearNum],[Grade],[Term],[CndtScore],[CndtPeerCode],[CndtPeerName],[CndtWeek],[CndtType]) values(@StdCode,@YearNum,@Grade,@Term,@CndtScore,@CndtPeerCode,@CndtPeerName,@CndtWeek,@CndtType);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateConductInfo = "UPDATE [ConductInfo] SET [StdCode] = @StdCode,[YearNum] = @YearNum,[Grade] = @Grade,[Term] = @Term,[CndtScore] = @CndtScore,[CndtPeerCode] = @CndtPeerCode,[CndtPeerName] = @CndtPeerName,[CndtWeek] = @CndtWeek,[CndtType] = @CndtType WHERE  [Id]=@Id";

		private static readonly string SqlDeleteConductInfo = "DELETE [ConductInfo] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllConductInfo = "SELECT [Id], [StdCode],[YearNum],[Grade],[Term],[CndtScore],[CndtPeerCode],[CndtPeerName],[CndtWeek],[CndtType] FROM [ConductInfo]";
		#endregion

		#region IConductInfoProvider Members
		public Core.Business.ConductInfo Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectConductInfo);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.ConductInfo conductInfo = new Core.Business.ConductInfo();

				if (!reader.IsDBNull(0)) conductInfo.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) conductInfo.StdCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) conductInfo.YearNum = reader.GetString(2);
				if (!reader.IsDBNull(3)) conductInfo.Grade = reader.GetString(3);
				if (!reader.IsDBNull(4)) conductInfo.Term = reader.GetInt32(4);
				if (!reader.IsDBNull(5)) conductInfo.CndtScore = reader.GetInt32(5);
				if (!reader.IsDBNull(6)) conductInfo.CndtPeerCode = reader.GetString(6);
				if (!reader.IsDBNull(7)) conductInfo.CndtPeerName = reader.GetString(7);
				if (!reader.IsDBNull(8)) conductInfo.CndtWeek = reader.GetInt32(8);
				if (!reader.IsDBNull(9)) conductInfo.CndtType = reader.GetString(9);

				return conductInfo;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.ConductInfo conductInfo)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StdCode", SqlDbType.NVarChar, conductInfo.StdCode);
			sql.AddParameter("@YearNum", SqlDbType.NVarChar, conductInfo.YearNum);
			sql.AddParameter("@Grade", SqlDbType.NVarChar, conductInfo.Grade);
			sql.AddParameter("@Term", SqlDbType.Int, conductInfo.Term);
			sql.AddParameter("@CndtScore", SqlDbType.Int, conductInfo.CndtScore);
			sql.AddParameter("@CndtPeerCode", SqlDbType.NVarChar, conductInfo.CndtPeerCode);
			sql.AddParameter("@CndtPeerName", SqlDbType.NVarChar, conductInfo.CndtPeerName);
			sql.AddParameter("@CndtWeek", SqlDbType.Int, conductInfo.CndtWeek);
			sql.AddParameter("@CndtType", SqlDbType.NVarChar, conductInfo.CndtType);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertConductInfo);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) conductInfo.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.ConductInfo conductInfo)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StdCode", SqlDbType.NVarChar, conductInfo.StdCode);
			sql.AddParameter("@YearNum", SqlDbType.NVarChar, conductInfo.YearNum);
			sql.AddParameter("@Grade", SqlDbType.NVarChar, conductInfo.Grade);
			sql.AddParameter("@Term", SqlDbType.Int, conductInfo.Term);
			sql.AddParameter("@CndtScore", SqlDbType.Int, conductInfo.CndtScore);
			sql.AddParameter("@CndtPeerCode", SqlDbType.NVarChar, conductInfo.CndtPeerCode);
			sql.AddParameter("@CndtPeerName", SqlDbType.NVarChar, conductInfo.CndtPeerName);
			sql.AddParameter("@CndtWeek", SqlDbType.Int, conductInfo.CndtWeek);
			sql.AddParameter("@CndtType", SqlDbType.NVarChar, conductInfo.CndtType);
			sql.AddParameter("@Id", SqlDbType.Int, conductInfo.Id);
			sql.ExecuteSql(SqlUpdateConductInfo);
		}

		public void Delete(Core.Business.ConductInfo conductInfo)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, conductInfo.Id);
			sql.ExecuteSql(SqlDeleteConductInfo);
		}

		public IList<Core.Business.ConductInfo> GetAllConductInfo()
		{
			IList<Core.Business.ConductInfo> conductInfolist = new List<Core.Business.ConductInfo>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllConductInfo);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.ConductInfo conductInfo = new Core.Business.ConductInfo();

					if (!reader.IsDBNull(0)) conductInfo.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) conductInfo.StdCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) conductInfo.YearNum = reader.GetString(2);
					if (!reader.IsDBNull(3)) conductInfo.Grade = reader.GetString(3);
					if (!reader.IsDBNull(4)) conductInfo.Term = reader.GetInt32(4);
					if (!reader.IsDBNull(5)) conductInfo.CndtScore = reader.GetInt32(5);
					if (!reader.IsDBNull(6)) conductInfo.CndtPeerCode = reader.GetString(6);
					if (!reader.IsDBNull(7)) conductInfo.CndtPeerName = reader.GetString(7);
					if (!reader.IsDBNull(8)) conductInfo.CndtWeek = reader.GetInt32(8);
					if (!reader.IsDBNull(9)) conductInfo.CndtType = reader.GetString(9);

					conductInfo.MarkOld();
					conductInfolist.Add(conductInfo);
				}
				reader.Close();
			}
			return conductInfolist;
		}
		#endregion
	}
}
